package config

import (
	"fmt"
	"github.com/spf13/viper"
	"os"
	"path/filepath"
	"pkg/cfg"
	"strings"
)

var (
	AppName   string
	Address   string
	MysqlDsn  string
	Host      string
	WxPemPath string
	WxKeyPath string
)

const (
	APPID     = "wx5b58503deb70471d"
	APPSECRET = "7e15dc79137398ea60381d1012e2981f"
)

const (
	WXNotifyCallbackURL = "/api/wxCallback"       //微信异步通知接口
	RefundCallBackURL   = "/api/wxRefundCallback" //微信退款回调通知
	WxAppId             = "wx5b58503deb70471d"
	WxKey               = "xYNwJdHKJzHu344sOAMWMYP28oiWbD90"
	WxMerchantId        = "1603370423"
	ORDER_PAY_TTL       = 60 * 15
)

func init() {
	Host = "http://api.haocheng2020.com"
	AppName = "card-platform"
	WxPemPath = GetRootPath() + "/conf/cert/apiclient_cert.pem"
	WxKeyPath = GetRootPath() + "/conf/cert/apiclient_key.pem"
}

func GetRootPath() string {
	return getCurrentDirectory()
}

func getCurrentDirectory() string {
	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		fmt.Println(err)
	}
	return strings.Replace(dir, "\\", "/", 0)
}

func InitConfig() {
	viper.AddConfigPath(getCurrentDirectory() + "/conf")
	//viper.AddConfigPath("/Users/yunshengzhu/golang/swuee/src/card-platform/conf")
	viper.SetConfigName("app")
	viper.SetConfigType("yaml")
	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
	viper.AutomaticEnv()
	if err := viper.ReadInConfig(); err != nil {
		if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
			panic(fmt.Errorf("Fatal error config file: %s \n", err))
		}
	}

	Address = viper.GetString("SERVER.ADDRESS")
	MysqlDsn = viper.GetString("MYSQL.DSN")
	cfg.InitLogger(getCurrentDirectory(), viper.GetString("LOG.FILE"), viper.GetInt64("LOG.MAXSIZE"))
	cfg.InitLogLevel(viper.GetInt("LOG.LEVEL"))
	cfg.LogInfo(viper.GetString("LOG.FILE"))
}
